package demo.socket.protocol;

public class ProtocolSock {
	private static final int WAITING = 0;
	private static final int SENT = 1;
	private static final int SENTCLUE = 2;
	private static final int ANOTHER = 3;

	private static final int NUMJOKES = 5;

	private int state = WAITING;
	private int count = 0;

	private String[] clues = { "Gioi thieu", "Cac san pham", "Cac tinh nang",
			"Tu van", "Thong tin lien lac" };
	private String[] answers = {
			"Se gioi thieu ve cong ty cua chung toi!",
			"Trung bay mot so san pham cua cong ty!",
			"Neu nhung tinh nang noi bat cua cac san pham!",
			"Chung toi se tu van cho cac ban nhung san pham phu hop voi cac ban!",
			"O day se la dia chi lien lac cua cong ty chung toi!" };

	public String processIn(String theInput) {
		String theOutput = "";
		if (state == WAITING) {
			theOutput = "Xin chao mung cac ban den voi dich vu cham soc khach hang!!";
			state = SENT;
		} else if (state == SENT) {
			if (theInput.equalsIgnoreCase("day la dv gi?")) {
				theOutput = clues[count];
				state = SENTCLUE;
			} else {
				theOutput = "Ban phai hoi la: \"day la dv gi?\"! "
						+ "Try again.";
			}
		} else if (state == SENTCLUE) {
			if (theInput.equalsIgnoreCase("la gi?")) {
				theOutput = answers[count]
						+ " Ban co muon chuyen den dv khac? (y/n)";
				state = ANOTHER;
			} else {
				theOutput = "Ban phai hoi la: \"la gi?\"! " + "Try again.";
				state = SENTCLUE;
			}
		} else if (state == ANOTHER) {
			if (theInput.equalsIgnoreCase("y")) {
				theOutput = "Xin chao ban den voi DV tiep theo";
				if (count == (NUMJOKES - 1))
					count = 0;
				else
					count++;
				state = SENT;
			} else {
				theOutput = "Bye.";
				state = WAITING;
			}
		}

		return theOutput;
	}
}
